<?php

class Wcurl{

	/**
	 * CURL 获取内容
	 */
	public static function grapContentByCurl($url){
		$ch = curl_init(); 
		$timeout = 15000;
		//模拟设置浏览器头
		$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
	    $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
	    $header[] = "Cache-Control: max-age=0"; 
	    $header[] = "Connection: keep-alive"; 
	    $header[] = "Keep-Alive: 300"; 
	    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
	    $header[] = "Accept-Language: en-us,en;q=0.5"; 
	    $header[] = "Accept-Encoding: gzip";
	    $header[] = "Pragma: "; // browsers keep this blank. 

		curl_setopt($ch, CURLOPT_URL, $url); 
		//在HTTP请求中包含一个"User-Agent: "头的字符串 
		// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686; rv:22.0)');
		curl_setopt($ch, CURLOPT_USERAGENT, 'spider');
		//一个用来设置HTTP头字段的数组
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
		//设定返回的数据是否自动显示
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//HTTP请求头中"Accept-Encoding: "的值
	    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
		//当根据Location:重定向时，自动设置header中的Referer:信息 
	    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		//设置cURL允许执行的最长秒数
	    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	    //是否抓取跳转后的页面 
	    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
	    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
		$contents = curl_exec($ch); 
		curl_close($ch); 
		if (empty($contents)){
	       return $contents = 0;
	    }
		return $contents;
	}
    public static function itunes_search($keywod, $appstoreid)
    {
        $url = 'https://itunes.apple.com/WebObjects/MZStore.woa/wa/search?clientApplication=Software&media=software&term=' . urlencode($keywod);
        $result = self::curl_itunes($url);
        $result = json_decode($result, true);
        $results = @$result['pageData']['bubbles'][0]['results'];
        if (empty($results)) {
            return 0;
        }
        foreach ($results as $k => $item) {
            if ($item['id'] == $appstoreid) {
                return $k + 1;
            }
        }
        return 0;
    }

    public static function curl_itunes($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $header = array('X-Apple-Store-Front: 143465-19,29');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'AppStore/2.0 iOS/11.3.1 model/iPhone10,3 hwp/s5l8960x build/13F69 (6; dt:90)');
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}